<?php
if(!defined('WY_ROOT')) exit;

class wechart_class extends Wy_Model{
    function get_list(){
        $data=array();
        $result=$this->fetch_all('weixin_menu',$limit,$offset,$cons,'order by order_id asc,id desc');
        return $result;
    }
  
    /**
     * 获取users表中的一条数据
     * @param $id 用户id
     * @return array|bool 用户信息|false
     */
    function get_menuinfo_by_id($id){
        $row=$this->fetch_one('weixin_menu','id='.$id);
        if($row){
            return $row;
        }
        return false;
    }
	function get_materialurl(){
        $data=array();
        $result=$this->fetch_all('materialurl',$limit,$offset,$cons,'order by time_add desc');
        return $result;
    }
	
	function get_childnum_by_id($id){
		 $result=$this->db->query("select count(*) from ".DB_PREFIX."weixin_menu where parnet_id=".$id);
		 return $result;
	}
	function get_parentnum_by_id(){
		 $result=$this->db->query("select count(*) from ".DB_PREFIX."weixin_menu where parent_id=0");
		 return $result;
	}
	
	function getImgList($id){
		 if(empty($id)) {
            return false;
        }
        if(is_numeric($id)) {

            $_resInfo =$this->fetch_one('material','id='.$id);

            //判断是否有子图文

            $_resLists =$this->fetch_all('material',$limit,$offset,"parent_id=".$id,'order by id asc');
            
            if(empty($_resLists)) {
                return false;
            }

            //格式化数组，返回信息
            $_return = array();
            $_return[0]['Title'] = $_resInfo['title'];

            $_return[0]['PicUrl'] = $_resInfo['title_pic_url'];

            $_return[0]['Description'] = $_resInfo['contents'];

            if(empty($_resInfo['link_url_id'])) {

                $_return[0]['Url'] = '';

            } else if(strstr($_resInfo['link_url_id'], 'http:')) {

                $_return[0]['Url'] = $_resInfo['link_url_id'];

            } else {

                $_return[0]['Url'] = $_resInfo['link_url_id'];

            }

            if(count($_resLists) > 0) {

                foreach($_resLists as $k => $v) {

                    $kk = $k+1;

                    $_return[$kk]['Title'] = $v['title'];

                    $_return[$kk]['PicUrl'] =$v['title_pic_url'];

                    $_return[$kk]['Description'] = $v['contents'];

                    if(empty($v['link_url_id'])) {

                        $_return[$kk]['Url'] = '';

                    } else if(strstr($v['link_url_id'], 'http:')) {

                        $_return[$kk]['Url'] = $v['link_url_id'];

                    } else {

                        $_return[$kk]['Url'] = C('WWW_DOMAIN') . $v['link_url_id'];

                    }

                }

            }

        }
		 return $_return;
	}
	
	function getSelectHtml($type='',$select=''){
		$string = '<option value="">--请选择--</option>';
        //返回系统
        $_r_url=  $this->fetch_all('materialurl',$limit,$offset,$cons,'order by id desc');
        if(count($_r_url)) {
            $string .= '<optgroup label="---------------系统业务模块---------------"></optgroup>';
            foreach($_r_url as $k => $v) {
                $v['link_url'] = $v['link_url'];
                $_selected = $select == $v['link_url'] ? 'selected="selected"' : '';
                $string .= '<option value="' . $v['link_url'] . '" ' . $_selected . '>' . $v['link_name'] . '</option>';
            }
        }
        if($type == 1) {
            //返回素材的
            $_r = $this->fetch_all('material',$limit,$offset,"parent_id=0",'order by id desc');
            $string .= '<optgroup label="---------------自定义图文消息---------------"></optgroup>';
            if(count($_r)) {
                foreach($_r as $k => $v) {
                    $_where = array();
                    $_where['parent_id'] = $v['id'];
                    $_c =  $this->count('material',"parent_id=".$v['id']);
                    if($_c > 0) {
                        $_tmpArrStr = '多图文';
                    } else {
                        $_tmpArrStr = '单图文';
                    }
                    $_selected = $select == $v['id'] ? 'selected="selected"' : '';
                    $string .= '<option value="' . $v['id'] . '" '.$_selected.'>[' . $_tmpArrStr . ']' . $v['title'] . '</option>';
                }
            }
        }//end if($type == 1)
        return $string;
	}
	
	function returnKeyword($keywords='', $uid, $_data=''){
		//判断是否是使用了工具
            $_where = array();
            $_r = $this->fetch_all('user_attention',$limit,$offset,$cons,'order by id desc');
            //用户自定义关键词对应的id
            $_findKwordArr = '';
            if(count($_r) > 0) {
                foreach($_r as $k => $v) {
                    if(empty($v['keywords'])) {
                        continue;
                    }
                    $_tmpArr = explode(',', $v['keywords']);
                    if($v['kwords_match'] == 0) {
                        //精准匹配
                        foreach($_tmpArr as $kk => $vv) {
                            if($vv == $keywords) {
                                $_findKwordArr = $v;
                                break;
                            }
                        }
                    } else {
                        //模糊匹配
                        foreach($_tmpArr as $kk => $vv) {
                            if(strstr($keywords, $vv)) {
                                $_findKwordArr = $v;
                                continue;
                            }
                        }
                    }
                }
            }//end if(count($_r['lists']) > 0) {
       
        $_return = array();
        //跟据关键词，返回内容
		
        if(is_array($_findKwordArr)) {
            //判断返回类型
            if($_findKwordArr['replay_type'] == 0) {
                //文本回复
                $_return['type'] = 'text';
                $_return['text'] = $_findKwordArr['contents_txt'];
            } //else if($_findKwordArr['replay_type'] == 1) {
//                //图文回复
//                $_return['type'] = 'news';
//                $M_WxMaterial = Db::name('Material');
//				
//                $_return['news'] = $M_WxMaterial->getImgList($_findKwordArr['contents_img'], $uid);
//                $_return['news'][0]['url'] = $_return['news'][0]['url']."&x=".$_data['locationinfo']['x']."&y=".$_data['locationinfo']['y'];
//                file_put_contents("aaa.txt", $_data['locationinfo']['x']." ".$_data['locationinfo']['y']);
//            }
            return $_return;
        }
		
     
        
        return $_return;
	}
	
	//用户余额查询
	function get_user_balance($openid){
		//获取用户的ID
		$user_info=$this->fetch_one("users","openid='".$openid."'");
		
		if($user_info){
			//查询商户账户信息
			$user_balance=$this->fetch_one("usermoney","userid=".$user_info['id']);
			//查询用户登陆日志
			$logs=$this->fetch_all('userlogs',"0,1",$offset,"userid=".$user_info['id'],'order by id desc');
			//查询用户收款信息
			$info=$this->fetch_one("userinfo","userid=".$user_info['id']);
			$ptype=array("1"=>"银行卡收款","2"=>"支付宝收款");
			$user_logs=$logs[0];
			$return_msg="商户ID：".$user_info['id']."\n";
			$return_msg.="账户余额[不含今天]：".$user_balance['unpaid']."\n";	
			$return_msg.="收款姓名：".$info['realname']."\n";	
			$return_msg.="收款方式：".$ptype[$info['ptype']]."\n";	
			$return_msg.="收款账号：".$info['alipay']."\n";	
			$return_msg.="最后登陆时间：".$user_logs['logtime']."\n";	
		}else{
		  $return_msg="您还没有绑定您的账号，请在菜单中点击账号绑定，完成账号绑定再继续操作。";	
		}
		return $return_msg;
	}
	//今日售出
	function get_user_todayorder($openid){
		$user_info=$this->fetch_one("users","openid='".$openid."'");
		if($user_info){
		//查询用户订单
		$order_list=$this->fetch_all('orderlist',$limit,$offset,"userid=".$user_info['id']." AND is_pay=1 AND addtime > '".date("Y-m-d 00:00:00")."'",'order by id desc');   
		
		$buy_list=$this->fetch_all('buylist',$limit,$offset,"userid=".$user_info['id']." AND is_status=1 AND addtime > '".date("Y-m-d 00:00:00")."'",'order by id desc');        
		$money=0.00;
		$card_num=0;
		foreach($order_list as $key=>$item){
			$money=$money+$item['price'];
		}
		foreach($buy_list as $kk=>$ii){
			$card_num=$card_num+$ii['quantity'];
		}
		$num=count($order_list);
		$return_msg="截止当前\n";
		$return_msg.="今日收入：".$money."元\n";
		$return_msg.="成交订单数：".$num."单\n";
		$return_msg.="售出卡密数量：".$card_num."张\n";
		}else{
		  $return_msg="您还没有绑定您的账号，请在菜单中点击账号绑定，完成账号绑定再继续操作。";	
		}
		return $return_msg;
	}
	function is_user_bind($openid){
		$user_info=$this->fetch_one("users","openid='".$openid."'");
		if($user_info){
			return 1;
		}else{
		  return 0;
		}
	}
	
	function getorder($order_sn,$openid){
		$user_info=$this->fetch_one("users","openid='".$openid."'");
		if($user_info){
			$order_info=$this->fetch_one("buylist","orderid='".$order_sn."'");
			if(!$order_info){
				$return_msg="您输入的订单号不存在，请仔细核对后再试~";	
			}else{
				//判断该订单是否属于当前商户
				
				if($user_info['id']==$order_info['userid']){
					$status=array("0"=>"未付款","1"=>"已付款","2"=>"部分付款","4"=>"已退款");
					$order_status=$order_info['is_freeze']?'已冻结':$status[$order_info['is_status']];
					$channelname=$this->model('channels')->get_channelname_by_id(intval($order_info['channelid']));
                    $goodname = $this->model('goodlist')->get_goodname_by_goodid(intval($order_info['goodid']));
					$return_msg="订单号：".$order_info['orderid']."\n";	
					$return_msg.="交易时间：".$order_info['addtime']."\n";
					$return_msg.="商品名称：".$goodname."\n";
					$return_msg.="支付方式：".$channelname."\n";
					$return_msg.="订单金额：".$order_info['price']."\n";
					$return_msg.="购买者信息：".$order_info['contact']."\n";
					$return_msg.="订单状态：".$order_status."\n";	
				}else{
					$return_msg="该订单不属于当前微信绑定的商户，无法查看";	
				}
			}
		}else{
			$return_msg="您还没有绑定您的账号，请在菜单中点击账号绑定，完成账号绑定再继续操作。";	
		}
		return $return_msg;
	}
	

   
}
?>